import UnityEngine

enum BehaviorState:
	Idle
	Walking

class GroupingAI(MonoBehaviour):
	public speed as single
	
	groupingLocations as (GameObject)
	targetIndex as int
	
	behavior as BehaviorState
	
	idleStartTime as single
	idleDuration as int
	
	def Start():
		groupingLocations = GameObject.FindGameObjectsWithTag('GroupingPlace')
		targetIndex = Random.Range(0, len(groupingLocations))
		behavior = BehaviorState.Walking
		idleStartTime = Time.realtimeSinceStartup
		idleDuration = 0

	def Update():
		if behavior == BehaviorState.Idle:
			if Time.realtimeSinceStartup >= idleStartTime + idleDuration:
				groupingLocations[targetIndex].GetComponent(DiscussionManager).UnregisterGroupie(gameObject)
				rand as int
				while true:
					rand = Random.Range(0, len(groupingLocations))
					break if rand != targetIndex
				targetIndex = rand
				behavior = BehaviorState.Walking
		else:
			diff = groupingLocations[targetIndex].transform.position - transform.position
			diff.y = 0
			length = diff.magnitude
			if length < speed:
				transform.Translate(diff, Space.World)
				SwitchIdle(2)
				groupingLocations[targetIndex].GetComponent(DiscussionManager).RegisterGroupie(gameObject)
			else:
				move = diff * speed / length
				transform.Translate(Vector3(move.x, 0, move.z), Space.World)
				
	def SwitchIdle(seconds as int):
		behavior = BehaviorState.Idle
		idleStartTime = Time.realtimeSinceStartup
		idleDuration = seconds
